package rauguelu.feria.tapas;

import java.io.IOException;

import rauguelu.programaEventos.R;
import rauguelu.programaEventos.R.layout;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public class FeriaVinos extends ListActivity{

	BaseDatosFeriasHelper miBBDDHelper;
	
	 @Override
	    public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.feria_vino);
	        
	    	Typeface font = Typeface.createFromAsset(getAssets(), "VSVBPSB.TTF");
		    TextView titulo_vinos = (TextView) findViewById(R.id.titulo_vinos);
		    titulo_vinos.setTypeface(font);
	        
	    	crearBBDDTapas();
	    	
	        // Get all of the rows from the database and create the item list
	    	Cursor vinosCursor = getVinos();
	    	 
	        startManagingCursor(vinosCursor);

	        // Create an array to specify the fields we want to display in the list (only TITLE)
	        String[] from = new String[]{"NOMBRE","DESCRIPCION"};

	        // and an array of the fields we want to bind those fields to (in this case just text1)
	        int[] to = new int[]{R.id.vino,R.id.descripcion};

	        // Now create a simple cursor adapter and set it to display
	        SimpleCursorAdapter vinos = 
	            new FeriaAdapter(this, R.layout.vinos_row, vinosCursor, from, to,R.id.imagen,3);
	        setListAdapter(vinos);
	        
	 }
	 
	 public Cursor getVinos() {
			Cursor listaVinos = null;
					
			miBBDDHelper.abrirBaseDatosFerias();
			listaVinos = miBBDDHelper.fetchAllVinos();
			//miBBDDHelper.close();
			return listaVinos;
		}
	 
	 public void crearBBDDTapas() {
			miBBDDHelper = new BaseDatosFeriasHelper(this);
			try {
				miBBDDHelper.crearDataBaseFerias();
			} catch (IOException ioe) {
				throw new Error("Unable to create database");
			}
		}
	 
	 class FeriaAdapter extends SimpleCursorAdapter {
			
			private Cursor c;
			private int labelImage;
			private int posImagen;
			private Context context;
			
			FeriaAdapter(Context context,int layout,Cursor c, String[] from, int[] to,int labelImage,int posImagen) {
		    super(context,layout, c, from, to);
		    this.c=c;
		    this.labelImage=labelImage;
		    this.posImagen = posImagen;
		    this.context = context;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
		    View row = super.getView(position, convertView, parent);
		    ImageView icon = (ImageView) row.findViewById(labelImage);
		    TextView vino = (TextView) row.findViewById(R.id.vino);
		   
		    
		    Typeface font = Typeface.createFromAsset(getAssets(), "VSVBPSB.TTF");
		    vino.setTypeface(font);
		    
		    row.setOnClickListener(new OnClickListener() {
				

				@Override
				public void onClick(View v) {
					  int position = getListView().getPositionForView(v);
					  long id = getListView().getItemIdAtPosition(position);
					  Intent i = new Intent(getListView().getContext(), VinoDetalle.class);
				      i.putExtra("_id", id);
				      startActivityForResult(i, 1);
				}
			});
		    
		    icon.setImageResource(context.getResources().getIdentifier(c.getString(posImagen), "drawable", context.getPackageName()));
		    return (row);
		 }


		}
}
